package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CodeGenSceneTemplateDao;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenSceneTemplate;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.SqliteUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2025/8/21 07:57
 */
public class CodeGenSceneTemplateDaoImpl extends BaseDAO<CodeGenSceneTemplate> implements CodeGenSceneTemplateDao<CodeGenSceneTemplate> {

    private static CodeGenSceneTemplateDaoImpl instance;

    private CodeGenSceneTemplateDaoImpl() {

    }

    public static synchronized CodeGenSceneTemplateDaoImpl getInstance() {
        if (null == instance) {
            instance = new CodeGenSceneTemplateDaoImpl();
        }
        return instance;
    }

    /**
     * 名称不能包含$符
     * 以$作为分割
     */
    private static final String SELECT_SQL = " select id,scene,template,json_scene_template,create_time,update_time from codegen_scenetemplate ";

    @Override
    public String tableName() {
        return "codegen_scenetemplate";
    }

    @Override
    public void insert(Connection conn, CodeGenSceneTemplate[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into codegen_scenetemplate(scene, template,json_scene_template,create_time,update_time) values(?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CodeGenSceneTemplate codeGenSceneTemplate = ary[0];
            update(conn, sql, codeGenSceneTemplate.getScene(), codeGenSceneTemplate.getTemplate(), codeGenSceneTemplate.getJsonSceneTemplate(), codeGenSceneTemplate.getCreateTime(), codeGenSceneTemplate.getUpdateTime());
        } else {
            Object[][] args = new Object[size][5];
            for (int i = 0; i < size; i++) {
                CodeGenSceneTemplate codeGenSceneTemplate = ary[i];
                args[i][0] = codeGenSceneTemplate.getScene();
                args[i][1] = codeGenSceneTemplate.getTemplate();
                args[i][2] = codeGenSceneTemplate.getJsonSceneTemplate();
                args[i][3] = codeGenSceneTemplate.getCreateTime();
                args[i][4] = codeGenSceneTemplate.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CodeGenSceneTemplate insert(Connection conn, CodeGenSceneTemplate codeGenSceneTemplate) {
        String sql = "insert into codegen_scenetemplate(scene, template,json_scene_template,create_time,update_time) values(?,?,?,?,?)";
        update(conn, sql, codeGenSceneTemplate.getScene(), codeGenSceneTemplate.getTemplate(), codeGenSceneTemplate.getJsonSceneTemplate(), codeGenSceneTemplate.getCreateTime(), codeGenSceneTemplate.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CodeGenSceneTemplate updateT) {
        String sql = "delete from codegen_scenetemplate where scene = ? AND template = ? ";
        return update(conn, sql, updateT.getScene(), updateT.getTemplate()) > 0;
    }

    @Override
    public void update(Connection conn, CodeGenSceneTemplate[] ary) {

    }

    @Override
    public void update(Connection conn, CodeGenSceneTemplate codeGenSceneTemplate) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CodeGenSceneTemplate updateT) {
        String sql = "update codegen_scenetemplate SET json_scene_template = ?, update_time = ? where scene = ? AND template = ? ";
        return update(conn, sql, updateT.getJsonSceneTemplate(), DateUtils.currentTime(), updateT.getScene(), updateT.getTemplate()) > 0;
    }

    @Override
    public CodeGenSceneTemplate findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CodeGenSceneTemplate findByUnique(Connection conn, CodeGenSceneTemplate queryT) {
        String sql = SELECT_SQL + " where scene = ? AND template = ? limit 1";
        List<CodeGenSceneTemplate> re = queryList(conn, sql, queryT.getScene(), queryT.getTemplate());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CodeGenSceneTemplate> queryRelate(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where scene IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }
}
